package com.itheima.HomeWork;

import java.util.LinkedHashMap;
import java.util.Objects;
import java.util.function.BiConsumer;

public class Demo05 {
    public static void main(String[] args) {
        LinkedHashMap<Student, String> students = new LinkedHashMap<Student, String>();
        students.put(new Student("100","张三",221),"北部");
        students.put(new Student("100","李四",221),"南部");
        students.put(new Student("101","王五",221),"东部");
        students.put(new Student("102","马六",221),"西部");

        students.forEach(new BiConsumer<Student, String>() {
            @Override
            public void accept(Student student, String s) {
                System.out.println(student.getName()+","+student.getId()+","+student.getScore()+s);
            }
        });
    }
}

class Student{
    private String id;
    private String name;
    private int score;

    public Student() {
    }

    public Student(String id, String name, int score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        //仅要求学号不同，才会不等 姓名可以要求一样
        if(Objects.equals(id,student.id)){
            System.out.println(name+"和"+student.getName()+"的id相同，不能重复添加");
            return true;//是否重复
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);//存相同的地方
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}